Imports System
Imports System.Data
Imports Microsoft.ApplicationBlocks.Data

Namespace RTP.HTMLNewsletter

    Public Class SqlDataProvider

        Inherits DataProvider

#Region "Private Data"
        Private Const ProviderType As String = "data"
        Private _providerConfiguration As Framework.Providers.ProviderConfiguration = Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)
        Private _connectionString As String
        Private _providerPath As String
        Private _objectQualifier As String
        Private _databaseOwner As String
#End Region

#Region "Base Methods"
        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Framework.Providers.Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Framework.Providers.Provider)

            ' This code handles getting the connection string from either the connectionString / appsetting section and uses the connectionstring section by default if it exists.  
            ' Get Connection string from web.config
            _connectionString = DotNetNuke.Common.Utilities.Config.GetConnectionString()

            ' If above funtion does not return anything then connectionString must be set in the dataprovider section.
            If _connectionString = "" Then
                ' Use connection string specified in provider
                _connectionString = objProvider.Attributes("connectionString")
            End If

            _providerPath = objProvider.Attributes("providerPath")

            _objectQualifier = objProvider.Attributes("objectQualifier")
            If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
                _objectQualifier += "_"
            End If

            _databaseOwner = objProvider.Attributes("databaseOwner")
            If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
                _databaseOwner += "."
            End If

        End Sub

        Public ReadOnly Property ConnectionString() As String
            Get
                Return _connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return _providerPath
            End Get
        End Property

        Public Overrides ReadOnly Property ObjectQualifier() As String
            Get
                Return _objectQualifier
            End Get
        End Property

        Public Overrides ReadOnly Property DatabaseOwner() As String
            Get
                Return _databaseOwner
            End Get
        End Property
#End Region

#Region "Newsletter Methods"

        Public Overrides Sub DeleteNewsletter(ByVal NLID As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_DeleteNewsletter", NLID)
        End Sub

        Public Overrides Function GetNewslettersByUserID(ByVal UserID As Integer, ByVal PortalID As Integer, ByVal AdminUser As Boolean) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewslettersByUserID", UserID, PortalID, AdminUser), IDataReader)
        End Function

        Public Overrides Function GetNewsletterbyID(ByVal itemid As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewsletterbyID", itemid), IDataReader)
        End Function

        Public Overrides Function GetNewslettersForEdit(ByVal ModuleID As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewslettersForEdit", ModuleID), IDataReader)
        End Function

        Public Overrides Function AddNewsletter(ByVal NLSummary As String, ByVal NLBody As String, ByVal NLDesc As String, ByVal NLAttachment As String, ByVal NLRoleSent As Integer, _
                                                ByVal NLUserID As Integer, ByVal NLEmail As String, ByVal ModuleID As Integer, ByVal ArticleID As Integer, ByVal ArticleTabModuleID As Integer, _
                                                ByVal Published As Boolean, ByVal PublishDate As DateTime, ByVal PortalID As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_AddNewsletter", NLSummary, NLBody, NLDesc, NLAttachment, NLRoleSent, NLUserID, NLEmail, ModuleID, ArticleID, ArticleTabModuleID, Published, PublishDate, PortalID), Integer)
        End Function

        Public Overrides Sub UpdateNewsletter(ByVal NLID As Integer, ByVal NLSummary As String, ByVal NLBody As String, ByVal NLDesc As String, ByVal NLAttachment As String, ByVal NLRoleSent As Integer, ByVal NLUserID As Integer, ByVal NLEmail As String, ByVal ModuleID As Integer, ByVal ArticleID As Integer, ByVal ArticleTabModuleID As Integer, ByVal Published As Boolean, ByVal PublishDate As DateTime)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_UpdateNewsletter", NLID, NLSummary, NLBody, NLDesc, NLAttachment, NLRoleSent, NLUserID, NLEmail, ModuleID, ArticleID, ArticleTabModuleID, Published, PublishDate)
        End Sub

#End Region

#Region "Newsletter User Methods"
        Public Overrides Function AddNewsletterUser(ByVal NLID As Integer, ByVal UserID As Integer, ByVal Ignore As Boolean, ByVal Sent As Boolean, ByVal UserEmail As String, ByVal NewsletterCategoryID As Integer, ByVal PublishDate As DateTime, ByVal PortalAliasID As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_AddNewsletterUser", NLID, UserID, Ignore, Sent, UserEmail, NewsletterCategoryID, PublishDate, PortalAliasID), Integer)
        End Function
        Public Overrides Sub UpdateNewsletterUser(ByVal NLID As Integer, ByVal UserID As Integer, ByVal Ignore As Boolean, ByVal Sent As Boolean, ByVal PublishDate As DateTime)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_UpdateNewsletterUser", NLID, UserID, Ignore, Sent, PublishDate)
        End Sub
        Public Overrides Function GetNewsletterUserUnsent() As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewsletterUserUnsent"), IDataReader)
        End Function
#End Region

#Region "Newsletter Template Functions"
        Public Overrides Function AddNewsletterTemplate(ByVal TLBody As String, ByVal TLName As String, ByVal PortalID As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_AddNewsletterTemplate", TLBody, TLName, PortalID), Integer)
        End Function
        Public Overrides Function GetNewsletterTemplates(ByVal PortalID As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewsletterTemplates", PortalID), IDataReader)
        End Function
        Public Overrides Function GetNewsletterTemplateById(ByVal TLID As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewsletterTemplatebyID", TLID), IDataReader)
        End Function
        Public Overrides Sub UpdateNewsletterTemplate(ByVal TLBody As String, ByVal TLID As Integer, ByVal PortalID As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_UpdateNewsletterTemplate", TLBody, TLID, PortalID)
        End Sub
        Public Overrides Sub DeleteNewsletterTemplate(ByVal TLID As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_DeleteNewsletterTemplate", TLID)
        End Sub
#End Region

#Region "Newsletter Categorys Functions"

        Public Overrides Function AddNewsletterCategory(ByVal NewsletterID As Integer, ByVal CategoryID As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_AddNewsletterCategory", NewsletterID, CategoryID), Integer)
        End Function
        Public Overrides Function GetNewsletterCategorysByNewsletterID(ByVal NewsletterID As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewsletterCategorysByNewsletterID", NewsletterID), IDataReader)
        End Function
        Public Overrides Function GetNewsletterCategoryByNewsletterIDCategoryID(ByVal NewsletterID As Integer, ByVal CategoryID As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_GetNewsletterCategoryByNewsletterIDCategoryID", NewsletterID, CategoryID), IDataReader)
        End Function
        Public Overrides Sub DeleteNewsletterCategory(ByVal NewsletterID As Integer, ByVal CategoryID As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_DeleteNewsletterCategory", NewsletterID, CategoryID)
        End Sub
#End Region

#Region "Ventrian News Articles Functions"

        Public Overrides Sub UpdateVentrianNAImages(ByVal ArticleID As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "Nexxus_HTMLNewsletter_UpdateVentrianNAImages", ArticleID)
        End Sub
#End Region
    End Class

End Namespace